<?php
class signModel extends  appModel{
	
	public function get_list($sort, $page, $limit = 10)
	{
		$where = "  where 1=1 and totalnum >0  ";
		
	
		$listPage = array('data' => '', 'pageLink' => '');
		
		
		$sql = "SELECT count(*) as num , sum(signnum) as signnum , sum(totalnum) as totalnum  FROM ".$this->table('sign_record').$where;
		
		$count = $this->getRow($sql);
		
		$sql = "SELECT  sum(signnum) as signnum   FROM ".$this->table('sign_record')." where totalnum >0 and lastTime >".strtotime(date("Y-m-d"));
		
		
		$count['signnum'] = $this->getOne($sql);
				
		
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('sign_record')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.wxid ".
				$where." ORDER BY lastTime DESC";
		
		$result = $this->show_page($sql, $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		$listPage['count'] = $count;
		if($result['result'])
		foreach ($result['result'] as $k => $v)
		{
			
			$listPage['data'][$k]['lastTime'] = date("Y-m-d H:i:s",$v['lastTime']);
				
		}
	
		return $listPage;
	
	}
	
	public function  persent_list($page){
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1 order by addtime desc ";
		$result=$this->get_page(" * ",$this->table('sign_persent'),$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('sign_persent');
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	public function del_persent($id){
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
		
			$sql="DELETE FROM ".$this->table('sign_persent')." WHERE  id='".$id."'";
		
			$res=$this->query($sql);
		
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
		}
		return $result;
	}
	public function get_persent($id){
		$sql = "SELECT * FROM ".$this->table("sign_persent")." WHERE id='".$id."' LIMIT 1";
		$result = $this->getRow($sql);
		return $result;		
	}
	
	public function persent_record_list($page){
		$listPage=array('data'=>'','pageLink'=>'');
		$where = "  p1.wxid=p2.wxid and p1.persent_id=p3.id  order by  p1.addtime desc ";
		$result=$this->get_page(" p1.* ,  p2.nickname,p3.name as persent_name,p3.number as persent_number ",$this->table('sign_persent_record')." as p1 , ".$this->table('wxusers')."  as p2 , ".$this->table("sign_persent")." as p3 ",$where,$page,30);
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('sign_persent_record');
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
}